package com.test.prolenders.dialog_prolenders;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.prolenders.R;
import com.test.prolenders.bean_prolenders.process_prolenders.ProcessInfo_prolenders;
import com.test.prolenders.config_prolenders.ProcessConfig_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import java.util.ArrayList;

public class ProcessBottomDialog_prolenders extends BottomSheetDialog {

    private LinearLayout groupLayout;

    private final Runnable applyRunnable;

    private final String page = "DIALOG_PROCESS";

    public ProcessBottomDialog_prolenders(Context context, Runnable applyRunnable){
        super(context, R.style.Theme_ProLenders_BottomDialogStyle);
        this.applyRunnable = applyRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_process_prolenders);

        getWindow().setBackgroundDrawableResource(R.color.transparent_prolenders);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        groupLayout = findViewById(R.id.dp_layoup_group);

        initProcessView();

        findViewById(R.id.dp_ib_close).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"CLOSE_CLICK","");
            dismiss();
        });

        findViewById(R.id.dp_btn_apply).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"APPLY_CLICK","");
            dismiss();
            if (applyRunnable!=null) applyRunnable.run();
        });

    }

    private void initProcessView(){
        groupLayout.removeAllViews();

        ArrayList<String> allSteps = ProcessInfo_prolenders.getInstance().getAllSteps();
        ArrayList<String> steps = ProcessInfo_prolenders.getInstance().getSteps();

        int i;
        for (i=0;i<allSteps.size();i++){
            String step = allSteps.get(i);
            String text = ProcessInfo_prolenders.getInstance().getStepTitle(i);
            addProcess(ProcessConfig_prolenders.getProcessImgResId(step),i+1,text,!steps.contains(step),false);
        }

        addProcess(R.drawable.img_process_dialog_submitloanneed_prolenders,++i,getContext().getString(R.string.dialog_process_submitloanneed_prolenders),false,false);
        addProcess(R.drawable.img_process_dialog_waitloan_prolenders,++i,getContext().getString(R.string.dialog_process_waitloan_prolenders),false,true);

    }

    private void addProcess(int imgResId,int num,String text,boolean isComplete,boolean isLast){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_process_dialog_prolenders,null);

        if (num == 1) view.findViewById(R.id.ipd_view_topline).setVisibility(View.INVISIBLE);
        if (isLast) view.findViewById(R.id.ipd_view_bottomline).setVisibility(View.INVISIBLE);

        ImageView stateIv = view.findViewById(R.id.ipd_iv_state);
        ImageView typeIv = view.findViewById(R.id.ipd_iv_type);
        TextView stepNumTv = view.findViewById(R.id.ipd_tv_step_num);
        TextView textTv = view.findViewById(R.id.ipd_tv_text);

        if (isComplete) stateIv.setBackgroundResource(R.drawable.img_process_completed_prolenders);
        stepNumTv.append(" "+(num<10 ? "0"+num:num));
        typeIv.setBackgroundResource(imgResId);
        textTv.setText(text);

        groupLayout.addView(view);
    }

}
